package controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;

import view.CreateEvent;

public class ActionCreateEvent implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		String name = ((JComponent) e.getSource()).getName();

		boolean flag = true;
		switch (name){
		case "contatto":{
			JButton j = new JButton();
			Color colore = j.getBackground();
			if (((JComponent) e.getSource()).getBackground()!=Color.green){
				((JComponent) e.getSource()).setBackground(Color.green);
				CreateEvent.invitato.add(Integer.valueOf(((JComponent) e.getSource()).getComponent(0).getName()));
			}
			else
			{
				for (int i=0;i<CreateEvent.invitato.size();i++){
					System.out.println(CreateEvent.invitato.get(i));
					if (Integer.valueOf(((JComponent) e.getSource()).getComponent(0).getName())==CreateEvent.invitato.get(i)){
						CreateEvent.invitato.remove(i);
						break;
					}
				}
				((JComponent) e.getSource()).setBackground(colore);
			}

			flag=false;
		}break;
		case "typeChange":{
			CreateEvent.setPhoto((String) CreateEvent.type.getSelectedItem());
		}break;
		case "next":{
			CreateEvent.confirmEventProp(true);
			int id = CreateEvent.Evento.getEvn_id();
			Engine.validateEvent(id);
			flag=false;
		}break;
		case "annulla":{
			Engine.restartHome("CreateEvent");
			flag=false;
		}break;
		case "upH":{
			if (CreateEvent.posH<23)
				CreateEvent.posH++;
			else
				CreateEvent.posH = 0;
		}break;
		case "downH":{
			if (CreateEvent.posH>0)
				CreateEvent.posH--;
			else
				CreateEvent.posH = 23;
		}break;
		case "upM":{
			if (CreateEvent.posM<55)
				CreateEvent.posM += 5;
			else
				CreateEvent.posM = 0;
		}break;
		case "downM":{
			if (CreateEvent.posM>0)
				CreateEvent.posM -= 5;
			else
				CreateEvent.posM = 55;
		}break;
		}
		if (flag){
			CreateEvent.confirmEventProp(false);
			CreateEvent.Uppdater();
			//CreateEvent.confirmEventProp(false);
		} else
			CreateEvent.confirmEventProp(false);
		((JComponent)e.getSource()).requestFocus();
		flag=true;

	}
}
